package com.spring.format.code.generation.util.file;

import java.util.Date;
import java.util.List;

import com.spring.format.code.generation.constants.Constants;
import com.spring.format.code.generation.constants.FileNameEnumType;
import com.spring.format.code.generation.mybatis.po.TablePO;
import com.spring.format.code.generation.mybatis.vo.CreateFileVO;
import com.spring.format.code.generation.mybatis.vo.JavaAttributeVO;
import com.spring.format.code.generation.util.CommonCodeUtil;
import com.spring.format.code.generation.util.FieldUtil;
import com.spring.format.code.generation.util.NameUtil;
import com.spring.format.code.generation.util.PathUtil;

/**
 * 文件生成帮助类
 * 
 * @author lenovo
 *
 */
public class ShopBizFileUtil {
	private ShopBizFileUtil() {

	}

	public static String content(CreateFileVO fileVO) {
		TablePO tablePO = fileVO.getTablePO();
		List<JavaAttributeVO> attrVOList = fileVO.getAttrVOList();
		// vo的类名
		String voName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.VO_NAME);
		// service api名称
		String serviceApiName = NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.SERVICE_API_NAME);
		// service impl 名称
		String serviceImplName = NameUtil.createPrivateName(tablePO.getTable_name(), FileNameEnumType.SERVICE_API_NAME);
		StringBuilder attribute = new StringBuilder();
		// 拼接文件头
		attribute.append(head(tablePO, serviceApiName, serviceImplName));

		StringBuilder methodStr = new StringBuilder();
		// 增加数据
		methodStr.append(insert(voName, serviceImplName));
		// 批量增加数据
		methodStr.append(insertBatch(voName, serviceImplName));
		// 删除数据
		methodStr.append(delete(serviceImplName));
		// 修改数据
		methodStr.append(update(voName, serviceImplName));
		// 更改数据状态
		methodStr.append(editStatus(serviceImplName));
		// 更改数据状态
		methodStr.append(editStatusBatch(serviceImplName));
		// 根据id查询
		methodStr.append(queryById(voName, serviceImplName));
		// 分页
		methodStr.append(page(voName, serviceImplName));
		// 根据or条件获取唯一数据
		// methodStr.append(queryUniqueByOr(voName, serviceImplName));
		// 根据or条件获取列表数据
		// methodStr.append(queryListByOr(voName, serviceImplName));
		// 获取列表数据
		methodStr.append(queryList(voName, serviceImplName));
		/** 特殊字段，特殊方法的追加代码(如排序字段，唯一索引字段，有自己方法的字段) **/
		for (JavaAttributeVO attriVO : attrVOList) {
			attribute.append(specialFieldAddition(attriVO, voName, serviceImplName));
		}
		attribute.append(methodStr).append(CommonCodeUtil.methodEnd(" //end class"));

		return attribute.toString();

	}

	/** 特殊字段需要特殊的方法追加 **/
	public static String specialFieldAddition(JavaAttributeVO attriVO, String voName, String serviceImplName) {
		StringBuilder specialFieldAddition = new StringBuilder();
		/** 自己更新方法 **/
		if (FieldUtil.getHaveselfupdatefieldlist().indexOf(attriVO.getName()) >= 0) {
			specialFieldAddition.append(queryByField(voName, serviceImplName, attriVO));
		}
		return specialFieldAddition.toString();
	}

	// 根据 字段 查询数据
	public static String queryByField(String voName, String serviceImplName, JavaAttributeVO attriVO) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(CommonCodeUtil.methodRemark("根据" + attriVO.getName() + "查询数据"))
				.append(CommonCodeUtil.methodResult("vo", voName, NameUtil.queryName(attriVO.getName()),
						attriVO.getType() + " " + attriVO.getName()))
				.append(CommonCodeUtil.resultShopStatement("vo", voName))
				.append(CommonCodeUtil.nullReturnShop(attriVO.getName())).append(CommonCodeUtil.paramVOList(voName))
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_LIST))
				.append(" if(null!= voList && 1==voList.size()){ \n ").append(" resInfo.setData(voList.get(0)); \n ")
				.append(" resInfo.setCode(ResInfoCode.RIC_SUCCEES); \n").append(" resInfo; \n ").append(" } \n ")
				.append(CommonCodeUtil.returnOnly()).append(" resInfo; \n ").append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 拼接文件头
	public static String head(TablePO tablePO, String serviceApiName, String serviceImplName) {
		StringBuilder attribute = new StringBuilder();
		attribute.append("package ")
				.append(PathUtil.createPackagePath(tablePO.getTable_name(), FileNameEnumType.BIZ_NAME))

				.append("/**\n").append("  *").append(tablePO.getTable_comment()).append("\n").append("  *@author ")
				.append(System.getProperties().getProperty("user.name")).append("\n").append("  *@version 1.0.0 ")
				.append(DateUtil.dateFormat(new Date(), DateUtil.DATE_FORMAT)).append(" \n").append("  */\n")
				.append("@Service(\"")
				.append(NameUtil.createSpringServiceName(
						NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.BIZ_NAME)))
				.append("\") \n").append("public class ")
				.append(NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.BIZ_NAME))
				.append("  implements ")
				.append(NameUtil.createFileName(tablePO.getTable_name(), FileNameEnumType.BIZ_API_NAME)).append("{ \n")
				.append(CommonCodeUtil.logStatement()).append(" @Resource\n").append(" private ").append(serviceApiName)
				.append(" ").append(serviceImplName).append(";\n");

		return attribute.toString();
	}

	// 新增数据
	public static String insert(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		// 增加数据
		methodStr.append("	/** 1.1 增加数据  */ \n ")
				.append(CommonCodeUtil.implMethodResultLogin("vo", voName, Constants.SERVICE_METHOD_ADD,
						voName + " vo "))
				.append("		log.debug(\"方法 ").append(Constants.SERVICE_METHOD_ADD)
				.append("执行 vo ={},loginVO={}\",vo,loginVO );\n	")
				.append(CommonCodeUtil.resultShopStatement("vo", voName)).append(CommonCodeUtil.voNullReturnShop())
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_ADD))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 批量新增数据
	public static String insertBatch(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		// 增加数据
		methodStr.append("	/** 1.2 批量增加数据  */ \n ")
				.append(CommonCodeUtil.implMethodResultLogin("?", voName, Constants.SERVICE_METHOD_ADD_BATCH,
						" List<" + voName + "> voList "))
				.append("	log.debug(\"方法 ").append(Constants.SERVICE_METHOD_ADD_BATCH)
				.append("执行 vo ={},loginVO={}\",vo,loginVO );\n	")
				.append(CommonCodeUtil.resultShopStatement("vo", voName)).append(CommonCodeUtil.voListNullReturnShop())
				.append(CommonCodeUtil.returnOnly()).append(CommonCodeUtil.serviceMethodCall(serviceImplName,
						Constants.SERVICE_METHOD_ADD_BATCH, " voList "))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 删除数据
	public static String delete(String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 2 根据id删除数据 */ \n ")
				.append(CommonCodeUtil.implMethodResultLogin("?", null, Constants.SERVICE_METHOD_DELETE_BY_ID,
						" Integer id "))
				.append(CommonCodeUtil.resultShopStatement("?", null)).append(CommonCodeUtil.idNullReturnShop())
				.append(CommonCodeUtil.returnOnly()).append(CommonCodeUtil.serviceMethodCall(serviceImplName,
						Constants.SERVICE_METHOD_DELETE_BY_ID, " id "))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 更新数据
	public static String update(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 3 更新数据信息 */ \n  ")
				.append(CommonCodeUtil.implMethodResultLogin("?", voName, Constants.SERVICE_METHOD_UPDATE,
						voName + " vo "))
				.append(CommonCodeUtil.resultShopStatement("?", null)).append(CommonCodeUtil.voNullReturnShop())
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_UPDATE))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 根据id查询数据
	public static String queryById(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 4 根据id查询数据*/ \n ")
				.append(CommonCodeUtil.implMethodResult("vo", voName, Constants.SERVICE_METHOD_QUERY_BY_ID,
						" Integer id "))
				.append(CommonCodeUtil.resultShopStatement("vo", voName)).append(CommonCodeUtil.idNullReturnShop())
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_BY_ID, " id "))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 查询分页信息
	public static String page(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		// 查询分页
		methodStr.append("	/** 5 查询分页*/ \n  ")
				.append(CommonCodeUtil.implMethodResult("list", voName, Constants.SERVICE_METHOD_QUERY_PAGE,
						voName + " vo "))
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_PAGE))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 更改数据状态
	public static String editStatus(String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 6 根据ids 更改数据状态 */ \n ")
				.append(CommonCodeUtil.implMethodResultLogin("?", null, Constants.SERVICE_METHOD_EDIT_STATUS,
						"  Integer state,Integer id "))
				.append(CommonCodeUtil.resultShopStatement("?", null))
				.append("  		if (null == id||null==state) { \n ")
				.append("  log.debug(\"id:{},state:{} 参数为空，直接返回\",id,state); \n")
				.append("            resInfo.setMessage(\"参数为空\"); \n ").append("  return resInfo; \n } \n ")
				.append(CommonCodeUtil.returnOnly()).append(CommonCodeUtil.serviceMethodCall(serviceImplName,
						Constants.SERVICE_METHOD_EDIT_STATUS, "state,id"))
				.append(CommonCodeUtil.methodEnd());

		return methodStr.toString();
	}

	// 根据or条件查询唯一数据
	public static String queryUniqueByOr(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 7 根据or条件获取唯一数据*/ \n ")
				.append(CommonCodeUtil.implMethodResult("vo", voName, Constants.SERVICE_METHOD_QUERY_UNIQUE_BY_OR,
						voName + " vo "))
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_UNIQUE_BY_OR))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 根据or条件查询列表数据
	public static String queryListByOr(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		// 根据or条件获取数据集合
		methodStr.append("	/** 8 根据 or 条件获取数据集合 */ \n ")
				.append(CommonCodeUtil.implMethodResult("vo", voName, Constants.SERVICE_METHOD_QUERY_LIST_BY_OR,
						voName + " vo "))
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_LIST_BY_OR))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 查询列表数据
	public static String queryList(String voName, String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		// 根据or条件获取数据集合
		methodStr.append("	/**9获取数据集合 */ \n ")
				.append(CommonCodeUtil.implMethodResult("list", voName, Constants.SERVICE_METHOD_QUERY_LIST,
						voName + " vo "))
				.append(CommonCodeUtil.returnOnly())
				.append(CommonCodeUtil.serviceMethodCall(serviceImplName, Constants.SERVICE_METHOD_QUERY_LIST))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}

	// 批量更改数据状态
	public static String editStatusBatch(String serviceImplName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	/** 10 根据ids 更改数据状态 */ \n ")
				.append(CommonCodeUtil.implMethodResultLogin("?", null, Constants.SERVICE_METHOD_EDIT_STATUS_BATCH,
						" Integer state,Integer[] ids, "))
				.append(CommonCodeUtil.returnOnly()).append(CommonCodeUtil.serviceMethodCall(serviceImplName,
						Constants.SERVICE_METHOD_EDIT_STATUS_BATCH, "state,ids"))
				.append(CommonCodeUtil.methodEnd());
		return methodStr.toString();
	}
}
